<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./vue.js"></script>
    <style>
      .rate {
        display: flex;
        width: 300px;
        justify-content: space-around;
      }
      .rate div {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: #666;
      }
    </style>
  </head>
  <body>
    <div id="app">
      <div class="rate" @mouseleave="resTemp=res">
        <div
          v-for="x in 5"
          :style="{backgroundColor:x<=resTemp?'orange':'#666'}"
          @mouseover="flag?resTemp=x:null"
          @click="clickHandle"
        ></div>
      </div>
      <h1>{{res}}</h1>
      <button @click="flag=true,res=resTemp=0">重新选择</button>
    </div>
  </body>
  <script>
    let app = Vue.createApp({
      data() {
        return {
          res: 0, //记录最终结果
          resTemp: 0, //临时结果 用于鼠标划入的颜色
          flag: true,
        };
      },
      methods: {
        clickHandle() {
          if (this.flag) {
            this.res = this.resTemp;
            this.flag = false;
          }
        },
      },
    });
    app = app.mount("#app");
  </script>
</html>
